package cn.wolfcode.p2p.business.service.impl;
import cn.wolfcode.p2p.Exception.ErrorCode;
import cn.wolfcode.p2p.business.domain.SystemAccount;
import cn.wolfcode.p2p.business.mapper.SystemAccountMapper;
import cn.wolfcode.p2p.business.service.ISystemAccountService;
import cn.wolfcode.p2p.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

@Service
public class SystemAccountServiceImpl implements ISystemAccountService,ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private SystemAccountMapper systemAccountMapper;

    @Override
    public SystemAccount getSystemAccount() {
        return systemAccountMapper.selectSystemAccount();
    }

    @Override
    public void update(SystemAccount systemAccount) {
        AssertUtil.instance().isFalse(0 == systemAccountMapper.updateByPrimaryKey(systemAccount), ErrorCode.UPDATE_SYSTEM_ACCOUNT_EXCEPTION);
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        SystemAccount systemAccount = systemAccountMapper.selectSystemAccount();
        if (systemAccount == null) {
            systemAccount = new SystemAccount();
            systemAccountMapper.insert(systemAccount);
        }
    }
}
